var form, laydate, upload, table;
var $ ;
var wfId = parent.$("#viewForm").attr("wfId") || "";
var baseTaskId = parent.$("#viewForm").attr("baseTaskId") || "";
var mainId;

layui.use(['form', 'laydate', 'table', 'upload'], function () {
    form = layui.form;
    laydate = layui.laydate;
    table = layui.table;
    upload = layui.upload;
    $ = layui.$;
    $.ajax({
        url: "/wf/workflow/application/getFormView",
        data: {
            "wfId": wfId,
            "baseTaskId": baseTaskId
        },
        type: 'post',
        async: false,
        traditional: true,
        success: function (data) {
            if (data.OK === "OK") {
                $('form').append(data.data);
                $("input[lay-verify=date]").each(function () {
                    laydate.render({
                        elem: this
                    })
                });
                if($('[ext-type="img"]').length > 0){
                    loadImg();
                }

                if($('[ext-type="file"]').length > 0){
                    loadFile();
                }

                if($('[ext-type="datagrid"]').length > 0){
                    loadTable();
                }

                if ($('table[ext-type="suggestion"]').length > 0) {
                    $('table[ext-type="suggestion"]').each(function () {
                        var name = $(this).attr("name");
                        var val = $('#' + name).val();

                        if ($(this).find("textarea[ obj-type='opinion']").prop("disabled")) {
                            var tr = $(this).find("textarea[ obj-type='opinion']").parents("tr")[0];
                            $(tr).hide();
                            $(tr).next().hide();
                            $(tr).prev().hide();
                        }

                        if (val != null && val != undefined && val != "") {
                            var obj = JSON.parse(val);

                            if (obj != null && obj != "" && obj.length >= 1) {
                                var th = $(['<tr id="' + name + 'head' + '">'
                                    , '<td style="border: 1px solid #e6e6e6; background-color: #f2f2f2; text-align: center; width: 10%; color: #bbbbbb;">意见</td>'
                                    , '<td style="border: 1px solid #e6e6e6; background-color: #f2f2f2; text-align: center; width: 65%; color: #bbbbbb;">详细意见</td>'
                                    , '<td style="border: 1px solid #e6e6e6; background-color: #f2f2f2; text-align: center; width: 10%; color: #bbbbbb;">审批人</td>'
                                    , '<td style="border: 1px solid #e6e6e6; background-color: #f2f2f2; text-align: center; width: 15%; color: #bbbbbb;">审批时间</td>'
                                    , '</tr>'].join(''));
                                $(this).find('tbody').prepend(th);
                            }
                            obj.forEach(function (data, index) {
                                if (data != null) {
                                    var tr = $(['<tr style="text-align: center">'
                                        , '<td style="border: 1px solid #e6e6e6; text-align: left;">' + data.radio
                                        , '</td>'
                                        , '<td style="border: 1px solid #e6e6e6; text-align: left;">' + data.text
                                        , '</td>'
                                        , '<td style="border: 1px solid #e6e6e6; text-align: left;">' + data.user
                                        , '</td>'
                                        , '<td style="border: 1px solid #e6e6e6; text-align: left;">' + data.time
                                        , '</td>'
                                        , '</tr>'].join(''));

                                    $('#' + name + "head").after(tr)
                                }
                            });
                        }
                    });
                }

                form.render();
            } else {
                layer.msg('获取表单失败！', {
                    icon: 2,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                })
            }


        }
        ,
        error: function () {
            layer.msg('操作失败！', {
                icon: 1,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
            }, function () {
            })
        }
    });
});
